package ua.amsoft.brelok.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.database.Cursor;
import android.util.Log;

/**
 * @author Tair Mustafaiev
 */
public class BrelokUtils {
    
    private static final String TAG = "BrelokUtils";

    /**
     * @param object - object to be returned
     * @param cursor an {@link Cursor} object
     * @param fileColumn - column in DB table from which file will be fetched
     * @return T object from XML
     * @throws Exception thrown exception
     */
    public static <T> T parseObjectFromCursor(final Class<T> object, final Cursor cursor,
            final String fileColumn) throws Exception {
        if (cursor.moveToFirst()) {
            byte[] file = null;
            while (!cursor.isAfterLast()) {
                file = cursor.getBlob(cursor.getColumnIndex(fileColumn));
                cursor.moveToNext();
            }
            final Serializer serial = new Persister();
            final InputStream inputStream = new ByteArrayInputStream(file);
            final T reports = serial.read(object, inputStream);
            return reports;
        }
        return null;
    }
    
    public static String buildUrl(final String url, final String function) {
        
        try {
            final URL u = new URL(url);
        } catch (final MalformedURLException e) {
            Log.i(TAG, e.getLocalizedMessage());
        }
        return null;
    }
    
}
